package com.lingnan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.entity.po.CommunityComment;
import com.lingnan.entity.po.CommunityPost;
import com.lingnan.mapper.CommunityCommentMapper;
import com.lingnan.mapper.CommunityPostMapper;
import com.lingnan.service.ICommunityService;
import com.lingnan.utils.LoginContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class CommunityServiceImpl
        extends ServiceImpl<CommunityPostMapper, CommunityPost>
        implements ICommunityService {
    private final CommunityPostMapper postMapper;
    private final CommunityCommentMapper commentMapper;

    public CommunityServiceImpl(
            CommunityPostMapper postMapper,
            CommunityCommentMapper commentMapper
    ) {
        this.postMapper = postMapper;
        this.commentMapper = commentMapper;
    }


    @Transactional
    @Override
    public Long createComment(CommunityComment comment) {
        Long userId = Long.parseLong(LoginContextHolder.get("loginId").toString());
        comment.setUserId(userId);
        // 显式设置默认值
        comment.setLikeCount(0);
        comment.setStatus(1);
        comment.setCreatedAt(new Date());  // 使用LocalDateTime
        comment.setUpdatedAt(new Date());
        commentMapper.insert(comment);
        return comment.getId();
    }

    @Transactional
    @Override
    public boolean deletePost(Long postId) {
        Long currentUserId = Long.parseLong(LoginContextHolder.get("loginId").toString());
        CommunityPost post = postMapper.selectById(postId);

        // 检查帖子是否存在且当前用户是作者
        if (post == null || !currentUserId.equals(post.getUserId())) {
            return false;
        }

        post.setStatus(0);
        return postMapper.updateById(post) > 0;
    }

    public Page<CommunityPost> listPosts(int pageNum, int pageSize, String sortBy) {
        QueryWrapper<CommunityPost> wrapper = new QueryWrapper<CommunityPost>()
                .eq("status", 1);

        // 使用传统 switch 语句
        if ("like".equals(sortBy)) {
            wrapper.orderByDesc("like_count");
        } else if ("time_asc".equals(sortBy)) {
            wrapper.orderByAsc("created_at");
        } else {  // 默认时间倒序
            wrapper.orderByDesc("created_at");
        }

        return postMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public CommunityPost getPostById(Long postId){
        return postMapper.selectOne(new QueryWrapper<CommunityPost>()
                .eq("id",postId)
                .eq("status",1));
    }

    @Transactional
    @Override
    public Long createPost(CommunityPost post) {
        Long userId = Long.parseLong(LoginContextHolder.get("loginId").toString());
        post.setUserId(userId);
        // 显式设置默认值
        post.setLikeCount(0);
        post.setStatus(1);
        post.setCreatedAt(new Date());  // 使用LocalDateTime
        post.setUpdatedAt(new Date());
        postMapper.insert(post);
        return post.getId();
    }

    @Transactional
    @Override
    public boolean deleteComment(Long commentId) {
        Long currentUserId = Long.parseLong(LoginContextHolder.get("loginId").toString());
        CommunityComment comment = commentMapper.selectById(commentId);

        // 检查评论是否存在且当前用户是作者
        if (comment == null || !currentUserId.equals(comment.getUserId())) {
            return false;
        }

        comment.setStatus(0);
        return commentMapper.updateById(comment) > 0;
    }

    @Transactional
    @Override
    public Page<CommunityComment> listCommentsByPost(Long postId, int pageNum, int pageSize, String sortBy) {
        QueryWrapper<CommunityComment> wrapper = new QueryWrapper<CommunityComment>()
                .eq("post_id", postId)
                .eq("status", 1);

        if ("like".equals(sortBy)) {
            wrapper.orderByDesc("like_count");
        } else if ("time_asc".equals(sortBy)) {
            wrapper.orderByAsc("created_at");
        } else {  // 默认时间倒序
            wrapper.orderByDesc("created_at");
        }

        return commentMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
    }

    // 新增点赞方法
    @Transactional
    @Override
    public boolean likePost(Long postId) {
        UpdateWrapper<CommunityPost> wrapper = new UpdateWrapper<>();
        wrapper.setSql("like_count = like_count + 1")
                .eq("id", postId);
        return postMapper.update(null, wrapper) > 0;
    }

    @Transactional
    @Override
    public boolean likeComment(Long commentId) {
        UpdateWrapper<CommunityComment> wrapper = new UpdateWrapper<>();
        wrapper.setSql("like_count = like_count + 1")
                .eq("id", commentId);
        return commentMapper.update(null, wrapper) > 0;
    }


}
